package com.distribute.order.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.distribute.common.pojo.DistributeResult;
import com.distribute.common.util.ExceptionUtil;
import com.distribute.common.util.JsonUtils;
import com.distribute.pojo.Order;
import com.distribute.order.service.OrderService;

/**
 * <p>Title: OrderController</p>
 * <p>Description: </p>
 */
@Controller
public class OrderController {

	@Autowired
	private OrderService orderService;
	
	@RequestMapping(value="/create", method=RequestMethod.POST)
	@ResponseBody
	public DistributeResult createOrder(@RequestBody Order order) {
		try {
//			/*, order.getOrderShipping()*/
			DistributeResult result = orderService.createOrder(order, order.getOrderItem());
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return DistributeResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	}
	
	@RequestMapping(value="/olist/{orderId}")
	@ResponseBody
	public DistributeResult getOrderListById(@PathVariable long orderId) {
		try {
//			/*, order.getOrderShipping()*/
			Order order = orderService.getOrderById(orderId);
			return DistributeResult.ok(order);
		} catch (Exception e) {
			e.printStackTrace();
			return DistributeResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	}
	
	@RequestMapping(value="/ulist/{userId}")
	@ResponseBody
	public DistributeResult getOrderListByUserId(@PathVariable long userId,	@RequestParam(defaultValue="1")Integer page, 
			@RequestParam(defaultValue="6")Integer rows) {
		try {
//			/*, order.getOrderShipping()*/
			DistributeResult result = orderService.getOrderByUserId(userId, page, rows);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return DistributeResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	}
}
